package com.yt.user.service;

import com.yt.user.models.service.IPhoneInfoService;
import com.yt.user.mapper.PhoneInfoMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import com.yt.user.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.user.models.bo.PhoneInfoSaveBO;
import com.yt.user.models.dto.PhoneInfoDTO;
import com.yt.user.models.dto.CustomerPhoneDTO;
import com.yt.user.models.service.ICustomerPhoneService;

/**
 * 用户信息控制器
 *
 * @author yzz
 * @Date 2025-10-22 09:48:45
 */
@DubboService
public class PhoneInfoServiceImpl extends ServiceImpl<PhoneInfoMapper, PhoneInfoDTO> implements IPhoneInfoService {

    @Resource
    private YmlConfig ymlConfig;
    
    @Resource
    private ICustomerPhoneService customerPhoneService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(PhoneInfoSaveBO saveBO) {
        // 创建手机号信息
        PhoneInfoDTO dto = new PhoneInfoDTO();
        BeanUtils.copyProperties(saveBO,dto);
        // 使用insertAllColumn方法确保所有字段都被正确处理
        boolean phoneSaved = this.insertAllColumn(dto);
        
        // 如果指定了客户ID，则创建客户与手机号的关联关系
        if (phoneSaved && saveBO.getCustomerId() != null && !saveBO.getCustomerId().isEmpty()) {
            CustomerPhoneDTO customerPhoneDTO = new CustomerPhoneDTO();
            customerPhoneDTO.setCustomerId(saveBO.getCustomerId());
            customerPhoneDTO.setPhone(saveBO.getPhone());
            customerPhoneService.insert(customerPhoneDTO);
        }
        
        return phoneSaved;
    }
}